home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / gnu / gnulib / ohlutil / rmdir.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-08-24  |  3.9 KB  |  176 lines

  1. /* rmdir -- remove directories
  2.    Copyright (C) 1990 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 1, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /*
  19.  * MS-DOS port (c) 1990 by Thorsten Ohl, td12@ddagsi3.bitnet
  20.  *
  21.  * To this port, the same copying conditions apply as to the
  22.  * original release.
  23.  *
  24.  * IMPORTANT:
  25.  * This file is not identical to the original GNU release!
  26.  * You should have received this code as patch to the official
  27.  * GNU release.
  28.  *
  29.  * MORE IMPORTANT:
  30.  * This port comes with ABSOLUTELY NO WARRANTY.
  31.  *
  32.  * $Header: e:/gnu/fileutil/RCS/rmdir.c'v 1.3.0.2 90/06/29 00:47:09 tho Stable $
  33.  */
  34.  
  35. /* Usage: rmdir [-p] [+path] dir...
  36.  
  37.    Options:
  38.    -p, +path        Remove any parent dirs that are explicitly mentioned
  39.             in an argument, if they become empty after the
  40.             argument file is removed.
  41.  
  42.    David MacKenzie <djm@ai.mit.edu>  */
  43.  
  44. #include <stdio.h>
  45. #include <sys/types.h>
  46. #include "system.h"
  47. #include "getopt.h"
  48.  
  49. #ifdef MSDOS
  50. #include <direct.h>
  51. extern void main (int argc, char * *argv);
  52. extern void error (int status, int errnum, char *message, ...);
  53. static void remove_parents (char *path);
  54. static void strip_trailing_slashes (char *path);
  55. static void usage (void);
  56. #endif /* MSDOS */
  57.  
  58. #ifdef STDC_HEADERS
  59. #include <errno.h>
  60. #include <stdlib.h>
  61. #else
  62. extern int errno;
  63. #endif
  64.  
  65. void remove_parents ();
  66. void error ();
  67. void strip_trailing_slashes ();
  68. void usage ();
  69.  
  70. /* If nonzero, remove empty parent directories. */
  71. int empty_paths;
  72.  
  73. /* The name this program was run with. */
  74. char *program_name;
  75.  
  76. struct option longopts[] =
  77. {
  78.   {"path", 0, &empty_paths, 1},
  79.   {NULL, 0, NULL, 0}
  80. };
  81.  
  82. void
  83. main (argc, argv)
  84.      int argc;
  85.      char **argv;
  86. {
  87.   int errors = 0;
  88.   int optc;
  89.   int ind;
  90.  
  91.   program_name = argv[0];
  92. #ifdef MSDOS
  93.   strlwr (program_name);
  94. #endif /* MSDOS */
  95.   empty_paths = 0;
  96.  
  97.   while ((optc = getopt_long (argc, argv, "p", longopts, &ind)) != EOF)
  98.     {
  99.       switch (optc)
  100.     {
  101.     case 0:            /* Long option. */
  102.       break;
  103.     case 'p':
  104.       empty_paths = 1;
  105.       break;
  106.     default:
  107.       usage ();
  108.     }
  109.     }
  110.  
  111.   if (optind == argc)
  112.     usage ();
  113.   
  114.   for (; optind < argc; ++optind)
  115.     {
  116. #ifdef MSDOS
  117.       strlwr (argv[optind]);
  118. #endif /* MSDOS */
  119.  
  120.       strip_trailing_slashes (argv[optind]);
  121.       if (rmdir (argv[optind]) != 0)
  122.     {
  123.       error (0, errno, "%s", argv[optind]);
  124.       errors = 1;
  125.     }
  126.       else if (empty_paths)
  127.     remove_parents (argv[optind]);
  128.     }
  129.  
  130.   exit (errors);
  131. }
  132.  
  133. /* Remove any empty parent directories of `path'.
  134.    Replaces '/' characters in `path' with NULs. */
  135.  
  136. void
  137. remove_parents (path)
  138.      char *path;
  139. {
  140.   char *slash;
  141.  
  142.   do
  143.     {
  144.       slash = rindex (path, '/');
  145.       if (slash == NULL)
  146.     break;
  147.       /* Remove any characters after the slash, skipping any extra
  148.      slashes in a row. */
  149.       while (slash > path && *slash == '/')
  150.     --slash;
  151.       slash[1] = 0;
  152.     }
  153.   while (rmdir (path) == 0);
  154. }
  155.  
  156. /* Remove trailing slashes from PATH; they cause some system calls to fail. */
  157.  
  158. void
  159. strip_trailing_slashes (path)
  160.      char *path;
  161. {
  162.   int last;
  163.  
  164.   last = strlen (path) - 1;
  165.   while (last > 0 && path[last] == '/')
  166.     path[last--] = '\0';
  167. }
  168.  
  169. void
  170. usage ()
  171. {
  172.   fprintf (stderr, "Usage: %s [-p] [+path] dir...\n",
  173.        program_name);
  174.   exit (1);
  175. }
  176.